const HZJSocket = require('./HZJSocket');

var listener = new HZJSocket(8090);
let reciveDataFunc = function (socket,message) {
    OnRecievedData(listener,socket,message);
}
let endFunc = function (socket) {
    console.log(`endFunc: socket -- ${socket.remoteAddress}  结束`);
}
let errorFunc = function (socket,error) {
    error && console.log(`errorFunc: ${error}`);
}

listener.connection(reciveDataFunc,endFunc,errorFunc);

/**
 * 处理接收到的消息
 * @param {*} listener 监视器
 * @param {*} socket 
 * @param {*} bufferMessage 接收到的原始消息
 */
function OnRecievedData(listener,socket,bufferMessage)
{    
    console.log(bufferMessage.toString())
    if (bufferMessage.toString() == '已发送1'){
        listener.sendMessage(socket,'好的，再见');//发送消息
    }else{
        listener.sendMessage(socket,'请发送1');//发送消息
    }
}